package com.jason.quectel0805.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.jason.quectel0805.R;
import com.jason.quectel0805.adapters.WeChatAdapter;
import com.jason.quectel0805.fragments.WeChatContactFragment;
import com.jason.quectel0805.fragments.WeChatDiscoverFragment;
import com.jason.quectel0805.fragments.WeChatHomeFragment;
import com.jason.quectel0805.fragments.WeChatMineFragment;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

/**
 * @author：Jason
 * @date：2022/8/9 15:06
 * @email：1129847330@qq.com
 * @description: 需求：实现微信分栏展示效果
 */
public class WechatActivity extends AppCompatActivity {
    private ViewPager vpWechat;
    private TextView tvHome;
    private TextView tvContact;
    private TextView tvDiscover;
    private TextView tvMine;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wechat);

        //初始化所有控件
        initUI();
        //初始化ViewPager的适配器和数据源
        initViewPager();

        //设置监听器
        initListener();
    }

    private void initListener() {
        vpWechat.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        tvHome.setSelected(true);
                        tvContact.setSelected(false);
                        tvDiscover.setSelected(false);
                        tvMine.setSelected(false);
                        break;
                    case 1:
                        tvHome.setSelected(false);
                        tvContact.setSelected(true);
                        tvDiscover.setSelected(false);
                        tvMine.setSelected(false);
                        break;
                    case 2:
                        tvHome.setSelected(false);
                        tvContact.setSelected(false);
                        tvDiscover.setSelected(true);
                        tvMine.setSelected(false);
                        break;
                    case 3:
                        tvHome.setSelected(false);
                        tvContact.setSelected(false);
                        tvDiscover.setSelected(false);
                        tvMine.setSelected(true);
                        break;

                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initViewPager() {
        List<Fragment> list = new ArrayList<>();
        list.add(WeChatHomeFragment.newInstance());
        list.add(WeChatContactFragment.newInstance());
        list.add(WeChatDiscoverFragment.newInstance());
        list.add(WeChatMineFragment.newInstance());
        //初始化适配器/创建适配器
        WeChatAdapter adapter = new WeChatAdapter(getSupportFragmentManager(), list);
        //绑定适配器
        vpWechat.setAdapter(adapter);
        vpWechat.setCurrentItem(0);
        tvHome.setSelected(true);
    }

    private void initUI() {
        vpWechat = findViewById(R.id.vp_wechat);
        tvHome = findViewById(R.id.tv_wechat_home);
        tvContact = findViewById(R.id.tv_wechat_contact);
        tvDiscover = findViewById(R.id.tv_wechat_dicover);
        tvMine = findViewById(R.id.tv_wechat_mine);
    }

    /**
     * 点击跳转到home页面
     *
     * @param view
     */
    public void gotoHome(View view) {
        vpWechat.setCurrentItem(0);
    }

    public void gotoContact(View view) {
        vpWechat.setCurrentItem(1);
    }

    public void gotoDiscover(View view) {
        vpWechat.setCurrentItem(2);
    }

    public void gotoMine(View view) {
        vpWechat.setCurrentItem(3);
    }
}